package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.dao.TaskDao;
import com.lkd.entity.UserCountEntity;
import com.lkd.feign.UserService;
import com.lkd.vo.UserVO;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-03 9:47
 */
@Component
public class TaskJob {

    @Autowired
    private UserService userService;
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @XxlJob("countUserTask")
    public void countUserTask(){
         //1.扫描所有运营和运维人员
        List<UserVO> users = new ArrayList<>();
        List<UserVO> allRepairers = userService.getAllRepairers();
        users.addAll(allRepairers);
        List<UserVO> allOperators = userService.getAllOperators();
        users.addAll(allOperators);

        if(CollectionUtils.isEmpty(users)){
            return;
        }

        //2.遍历人员，获取工单数 ，保存到redis中  用户ID和工单数 1:10  2：5
        List<UserCountEntity> userIdTaskCount = taskDao.getUserIdTaskCount();
        HashMap<Long, Long> longLongHashMap = new HashMap<>();
        for (UserCountEntity userCountEntity : userIdTaskCount) {
            longLongHashMap.put(userCountEntity.getUserId(),userCountEntity.getCount());
        }
        for (UserVO user : users) {
            Long taskCount = longLongHashMap.get(user.getUserId().longValue());
            redisTemplate.opsForZSet().add(VMSystem.REGION_TASK_KEY_PREF
                    + user.getRegionId()
                    + user.getRoleCode(), user.getUserId(), taskCount == null ? 0 : taskCount);
        }

    }

    public static void main(String[] args) {
        int count = 100000;

        long time1 = System.currentTimeMillis();
        ArrayList<UserVO> userVOS = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            UserVO userVO = new UserVO();
            userVO.setUserId(i);
            userVOS.add(userVO);
        }
        ArrayList<UserCountEntity> userCountEntities = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            UserCountEntity userVO = new UserCountEntity();
            userVO.setUserId((long) i);
            userCountEntities.add(userVO);
        }

        HashMap<Long, Long> longLongHashMap = new HashMap<>();
        for (UserCountEntity userCountEntity : userCountEntities) {
            longLongHashMap.put(userCountEntity.getUserId(),userCountEntity.getCount());
        }

        for (UserVO user : userVOS) {
            Long aLong = longLongHashMap.get(user.getUserId().longValue());
        }

        long time2 = System.currentTimeMillis();
        System.out.println(time2 -time1);
    }
}
